<?php

# -- DÉBUT NOTE DE LICENCE -------------------------------------
#
# Ce fichier fait partie de Clicpage.
#
# Copyright (c) 2010 Francois (www.clicpage.net)
# Distribué selon les termes de la licence GPL version 2.0.
# Consultez le fichier LICENCE ou
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- FIN NOTE DE LICENCE ---------------------------------------

class cpGadgetsContenu
{

	public static function traiterContenu($match)
	{
		
		$typeMatch = array();
		$nbType = preg_match('#alt="(.+?)"#', $match[0], $typeMatch);
		if(!$nbType) {
			return $match[0];
		}
		
		switch($typeMatch[1])
		{
			case Maps:
				return self::traiterMaps($match[0]);
			break;
			
			case Youtube:
				return self::traiterYoutube($match[0]);
			break;
			
			default:
				return $match[0];
			break;
			
		}
	}
	
	
	public static function traiterMaps($match)
	{
		
		# Extraction des attributs
		$widthMatch = array();
		$heightMatch = array();
		$optionsMatch = array();
		$nbWidth = preg_match('#width="([0-9]+)"#', $match, $widthMatch);
		$nbHeight = preg_match('#height="([0-9]+)"#', $match, $heightMatch);
		$nbOptions = preg_match('#options="(.+?)"#', $match, $optionsMatch);
		
		if(!$nbOptions) {
			return $match;
		}
		
		if(!$nbWidth || !$nbHeight)
		{
			$width = 425;
			$height = 350;
		}
		else
		{
			$width = (integer) $widthMatch[1];
			$height = (integer) $heightMatch[1];
		}
		
		# Extraction des options
		$options = explode('|', $optionsMatch[1]);
		$latlng = explode(',', $options[0]);
		
		if(empty($latlng[0]) || empty($latlng[1]) || empty($options[1]) || empty($options[2]) || empty($options[3])) {
			return $match;
		}
		
		$latlng = $options[0];
		$zoom = (integer) $options[1];
		$adresse = $options[3];
		
		switch($options[2])
		{
			
			case 'satellite':
				$type = '&t=k';
			break;
			
			case 'hybrid':
				$type = '&t=h';
			break;
			
			case 'terrain':
				$type = '&t=p';
			break;
			
			default:
				$type = '';
			break;
			
		}
		
		
		# Code Google Maps
		$url = sprintf('http://maps.google.ca/maps?f=q&source=s_q&geocode=&q=%s&z=%s%s&ie=UTF8',
			urlencode($adresse),
			$zoom,
			$type		
		);
		
		return '<iframe width="'.$width.'" height="'.$height.'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$url.'&output=embed"></iframe><br />'.
		'<a style="font-size:0.8em;" href="'.$url.'&source=embed">Agrandir le plan</a>';
		
	}
	
	
	public static function traiterYoutube($match)
	{
		
		# Extraction des attributs
		$widthMatch = array();
		$heightMatch = array();
		$optionsMatch = array();
		$nbWidth = preg_match('#width="([0-9]+)"#', $match, $widthMatch);
		$nbHeight = preg_match('#height="([0-9]+)"#', $match, $heightMatch);
		$nbOptions = preg_match('#options="(.+?)"#', $match, $optionsMatch);
		
		if(!$nbOptions) {
			return $match;
		}
		
		if(!$nbWidth || !$nbHeight)
		{
			$width = 548;
			$height = 340;
		}
		else
		{
			$width = (integer) $widthMatch[1];
			$height = (integer) $heightMatch[1];
		}
		
		# Extraction des options
		$options = explode('|', $optionsMatch[1]);
		$similaires = $options[0] == '1' ? '?rel=1' : '?rel=0';
		$hd = $options[1] == '1' ? '&hd=1' : '&hd=0';
		$idVideo = $options[2];
		
		return '<iframe title="YouTube video player" class="youtube-player" type="text/html" width="'.$width.'" height="'.$height.'" src="http://www.youtube.com/embed/'.$idVideo.$similaires.$hd.'" frameborder="0"></iframe>';
		
	}
	
	
}